<template>
  <div class="department-list">
    <el-card>
      <div class="head-container">
        <!-- 搜索 -->
        <!-- 搜索 -->
        <el-input size="mini"  clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" />
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" >搜索</el-button>
        <!-- 新增 -->
        <div style="display: inline-block;margin: 0px 2px;">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="add"
          >新增</el-button>
          <el-button
            type="danger"
            class="filter-item"
            size="mini"
            icon="el-icon-refresh"
          >刷新</el-button>
          <el-button
            type="warning"
            class="filter-item"
            size="mini"
            icon="el-icon-s-operation"
            @click="$refs.initHospital.initVisible=true"
          >初始化医院</el-button>
        </div>
      </div>
        <init-hospital  ref="initHospital"></init-hospital>
        <!-- 提交表单 -->
      <eform :isAdd="isAdd" ref="form" @refresh="getList"></eform>
      <!-- 表单数据 -->
      <el-table  v-loading="loading" :data="list" max-height="700" size="small" border style="width: 100%; margin-top: 20px;">
          <el-table-column prop="locationName" label="医院名称" />
          <el-table-column prop="building" label="楼栋" />
          <el-table-column prop="floor" label="楼层" />
          <el-table-column prop="pointId" label="定位点id" />
          <el-table-column prop="showName" label="科室/房间" />
          <el-table-column label="科室照片">
            <template slot-scope="scope">
              <img v-if="scope.row.imgUrl != ''" :src="scope.row.imgUrl" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="pointName" label="关键字" />
          <el-table-column label="操作" width="205px" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
              <span style="display: inline-block;width: 3px;"></span>
              <el-popover
                :ref="scope.row.id"
                placement="top"
                width="180"
              >
                <p>确定删除本条数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                  <el-button  :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                </div>
                <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </el-popover>
            </template>
          </el-table-column>
      </el-table>
    </el-card>
    <!--分页组件-->
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      background
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import eform from './Form'
import departmentApi from '@/network/map/department.api'
import {mapActions} from 'vuex'
import InitHospital from '../../components/InitHospital'
export default {
  name: 'List',
  components: {
    eform,
    InitHospital,
  },
  data() {
    return {
     total: 0,
     page: 1,
     size: 10,
     locationId: '',
     building: '',
     floor: '',
     isAdd: true,
     loading: true,
     delLoading: false,
     list: [
     ],
    }
  },
  created() {
    this.getList()
  },
  methods: {
     ...mapActions('hospital',['saveHospitals']),
    async getList() {
      this.loading = true
      let {page, size, locationId,building,floor} = this
      const {pointList,count} = await departmentApi.getPointList({currentPage:page,count:size,locationId,building,floor})
      this.list = pointList.map(item => {
        if(item.pointImage) {
          item.imgUrl = item.pointImage.split(':$:')[1]
        } else {
          item.pointImage = ''
          item.imgUrl = ''
        }
        return item
      })
      this.total = count
       this.loading = false
    },
    sizeChange(size) {
      this.size = size
      this.getList()
    },
    pageChange(page) {
      this.page = page
      this.getList()
    },
    add() {
      this.isAdd = true
      const form = this.$refs.form
      form.isDialog = true
    },
    edit(data) {
      this.isAdd = false
      const form = this.$refs.form
      form.imageUrl = data.imgUrl
      form.ruleForm = {...data}
      form.isDialog = true
    },
    subDelete(id) {
      this.delLoading = true
      departmentApi.delPoint(id).then(() => {
        this.saveHospitals()
        this.delLoading = false
        this.$refs[id].doClose()
        this.getList();
        this.$message({message: '删除成功', type: 'success'})
        
      }).catch(() => {
        this.delLoading = false
        this.$refs[id].doClose()
      })
    }
  },
  
}
</script>

<style lang="scss" scoped>
.department-list {
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: space-between;
}
.el-card {
  flex: 1;
}
.head-container .filter-item {
      display: inline-block;
      vertical-align: middle;
    margin: 0 2px 10px 0;
}
img {
  width: 50px;
  height: auto;
}
</style>